<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head>
	<meta charset="UTF-8">
	<link href="/layui/css/layui.css" rel="stylesheet">
	<title>获取mybatis文件</title>
</head>
<body>
<div class="layui-container">
	<div class="layui-form layui-col-md6 layui-col-md-offset3 layui-form-pane" style="margin-top: 50px">
		<div class="layui-form-item">
			<div class="layui-row">
				<div class="layui-col-md3">
					<button class="layui-btn" id="add">新增数据源</button>
				</div>
				<div class="layui-col-md9">
					<input class="layui-input" id="dataShow" type="text" readonly/>
				</div>
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">表名</label>
			<div class="layui-input-block">
				<select id="selectTable" lay-filter="selectTable" lay-search>
					<option value="">请选择</option>
				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">选择文件</label>
			<div class="layui-input-block">
				<input type="checkbox" name="checkbox" title="mapper" value="mapper">
				<input type="checkbox" name="checkbox" title="model" value="model">
				<input type="checkbox" name="checkbox" title="dao" value="dao">
				<input type="checkbox" name="checkbox" title="service" value="service">
				<input type="checkbox" name="checkbox" title="controller" value="controller">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">包名</label>
			<div class="layui-input-block">
				<input type="text" placeholder="请输入包名" autocomplete="off"
				       class="layui-input" id="packageName">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">文件夹名称</label>
			<div class="layui-input-block">
				<input type="text" placeholder="文件夹名称" autocomplete="off"
				       class="layui-input" id="folderName">
			</div>
		</div>
		<div class="layui-form-item" style="margin-top: 50px">
			<div class="layui-input-block">
				<button class="layui-btn" id="build">生成</button>
			</div>
		</div>
		<div id="msg"></div>
	</div>
</div>
<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript">
	layui.use(['form'], function () {
		var form = layui.form, $ = layui.$;
		var url, params = {};
		var addIndex;

		$("#add").on('click', function () {
			addIndex = layer.open({
				type: 1,
				title: '新增数据源',
				offset: '10px',
				resize: false,
				scrollbar: false,
				move: false,
				area: ['350px', '440px'],
				content: $("#addPanel")
			});
		});

		form.on('submit(save)', function () {
			var ip = $("#ip").val();
			var port = $("#port").val();
			var userName = $("#userName").val();
			var password = $("#password").val();
			var dataBaseName = $("#dataBaseName").val();
			if (ip === "" || port === "" || userName === "" || password === "" || dataBaseName === "") {
				layer.msg("必填项不能为空", {offset: '100px'});
				return false;
			}
			url = '/source/save';
			params = {
				ip: ip,
				port: port,
				userName: userName,
				password: password,
				dataBaseName: dataBaseName
			};
			$.ajax({
				url: url,
				dataType: 'json',
				type: 'post',
				data: params,
				async: true,
				success: function (data) {
					layer.msg(data.msg, {offset: '100px'});
					$("#selectTable").empty();
					$("#selectTable").append("<option value=''>" + '请选择' + "</option>");
					layer.close(addIndex);
					$("#dataShow").val(ip + " | " + port + " | " + dataBaseName);
					$.each(data.data, function (i, v) {
						$("#selectTable").append("<option value=" + v + ">" + v + "</option>");
					});
					form.render('select');
				}
			});
		});

		form.on('submit(test)', function () {
			var ip = $("#ip").val();
			var port = $("#port").val();
			var userName = $("#userName").val();
			var password = $("#password").val();
			var dataBaseName = $("#dataBaseName").val();
			if (ip === "" || port === "" || userName === "" || password === "" || dataBaseName === "") {
				layer.msg("必填项不能为空", {offset: '100px'});
				return false;
			}
			url = '/source/testConn';
			params = {
				ip: ip,
				port: port,
				userName: userName,
				password: password,
				dataBaseName: dataBaseName
			};
			$.ajax({
				url: url,
				dataType: 'json',
				type: 'post',
				data: params,
				async: true,
				success: function (data) {
					layer.msg(data.msg, {offset: '100px'});
				}
			});
		});
		$("#build").on('click', function () {
			var tableName = $("#selectTable").val();
			var packageName = $("#packageName").val();
			var folderName = $("#folderName").val();
			/** dataBase begin **/
			var ip = $("#ip").val();
			var port = $("#port").val();
			var userName = $("#userName").val();
			var password = $("#password").val();
			var dataBaseName = $("#dataBaseName").val();
			/** dataBase end **/
			var obj = document.getElementsByName("checkbox");
			var fileType = "";
			var k;
			for (k in obj) {
				if (obj[k].checked)
					fileType += obj[k].value;
			}
			if (tableName === "") {
				layer.msg("请选择表名", {offset: '100px'});
				return false;
			}
			if (fileType === "") {
				layer.msg("请选择生成的文件", {offset: '100px'});
				return false;
			}
			if (packageName === "") {
				layer.msg("请输入包名", {offset: '100px'});
				return false;
			}
			if (folderName === "") {
				layer.msg("请输入文件夹名称", {offset: '100px'});
				return false;
			}
			var params = {
				ip: ip,
				port: port,
				userName: userName,
				password: password,
				dataBaseName: dataBaseName,
				tableName: tableName,
				fileType: fileType,
				packageName: packageName,
				folderName: folderName
			};
			var url = '/buildFile';
			$.ajax({
				url: url,
				dataType: 'json',
				type: 'post',
				data: params,
				async: true,
				success: function (data) {
					$.each(data, function (i, v) {
						$("#msg").html("<p style='color: red'>" + v + "</p>");
					})
				}
			});
		});
	});
</script>
</body>
<div style="padding: 20px 20px 0 20px;display: none;" id="addPanel">
	<div class="layui-form layui-form-pane">
		<div class="layui-form-item ">
			<label class="layui-form-label">数据源IP</label>
			<div class="layui-input-block">
				<input type="text" placeholder="请输入ip" autocomplete="off"
				       class="layui-input" id="ip" value="192.168.1.145">
			</div>
		</div>
		<div class="layui-form-item ">
			<label class="layui-form-label">端口号</label>
			<div class="layui-input-block">
				<input type="text" placeholder="请输入端口号，如：3306" autocomplete="off"
				       class="layui-input" id="port" value="3306">
			</div>
		</div>
		<div class="layui-form-item ">
			<label class="layui-form-label">数据库名称</label>
			<div class="layui-input-block">
				<input type="text" placeholder="请输入数据库名称" autocomplete="off"
				       class="layui-input" id="dataBaseName" value="robit">
			</div>
		</div>
		<div class="layui-form-item ">
			<label class="layui-form-label">用户名</label>
			<div class="layui-input-block">
				<input type="text" placeholder="请输入用户名" autocomplete="off"
				       class="layui-input" id="userName" value="robit">
			</div>
		</div>
		<div class="layui-form-item ">
			<label class="layui-form-label">密码</label>
			<div class="layui-input-block">
				<input type="password" placeholder="请输入密码" autocomplete="off"
				       class="layui-input" id="password" value="Robit@123">
			</div>
		</div>
		<div style="margin-top: 20px;text-align: center">
			<button class="layui-btn" lay-submit lay-filter="test">测试连接</button>
			<button class="layui-btn layui-btn-primary" lay-submit lay-filter="save">保存</button>
		</div>
	</div>
</div>
</html>